package com.example.mybatisplus.web.controller;

import com.example.mybatisplus.model.dto.RepairForm;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.stereotype.Controller;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.example.mybatisplus.common.JsonResponse;
import com.example.mybatisplus.service.KeeperService;
import com.example.mybatisplus.model.domain.Keeper;
import com.example.mybatisplus.model.domain.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 *
 *  前端控制器
 *
 *
 * @author zzy
 * @since 2024-06-28
 * @version v1.0
 */
@Controller
@RequestMapping("/api/keeper")
public class KeeperController {

    private final Logger logger = LoggerFactory.getLogger( KeeperController.class );

    @Autowired
    private KeeperService keeperService;

    /**
     * 描述：根据Id 查询
     *
     */
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    @ResponseBody
    public JsonResponse getById(@PathVariable("id") Long id)throws Exception {
        Keeper  keeper =  keeperService.getById(id);
        return JsonResponse.success(keeper);
    }

    /**
     * 描述：根据Id删除
     *
     */
    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
    @ResponseBody
    public JsonResponse deleteById(@PathVariable("id") Long id) throws Exception {
        keeperService.removeById(id);
        return JsonResponse.success(null);
    }


    /**
     * 描述：根据Id 更新
     *
     */
    @RequestMapping(value = "", method = RequestMethod.PUT)
    @ResponseBody
    public JsonResponse updateKeeper(Keeper  keeper) throws Exception {
        keeperService.updateById(keeper);
        return JsonResponse.success(null);
    }


    /**
     * 描述:创建Keeper
     *
     */
    @RequestMapping(value = "", method = RequestMethod.POST)
    @ResponseBody
    public JsonResponse create(Keeper  keeper) throws Exception {
        keeperService.save(keeper);
        return JsonResponse.success(null);
    }
    @PostMapping("/getUndoRepair")
    @ResponseBody
    public JsonResponse getUndoRepair(@RequestBody Dot dot) {
        dot.getData().put("ID", Long.parseLong((String) dot.getData().get("ID")));
        List<RepairForm> repairForms = keeperService.getUndoRepair(dot);
        int start = (dot.getPageNo()-1)*dot.getPageSize();
        int end = start+dot.getPageSize();
        Map<String, Object> map = new HashMap<>();
        map.put("totalNum", repairForms.size());
        repairForms = repairForms.subList(start, Math.min(end,repairForms.size()));
        return JsonResponse.success(repairForms, "success").setOtherData(map);
    }
    @PostMapping("/getDoneRepair")
    @ResponseBody
    public JsonResponse getDoneRepair(@RequestBody Dot dot) {
        dot.getData().put("ID", Long.parseLong((String) dot.getData().get("ID")));
        List<RepairForm> repairForms = keeperService.getDoneRepair(dot);
        int start = (dot.getPageNo()-1)*dot.getPageSize();
        int end = start+dot.getPageSize();
        Map<String, Object> map = new HashMap<>();
        map.put("totalNum", repairForms.size());
        repairForms = repairForms.subList(start, Math.min(end,repairForms.size()));
        return JsonResponse.success(repairForms, "success").setOtherData(map);
    }
    @PostMapping("/updateRepairKeeper")
    @ResponseBody
    public JsonResponse updateRepairKeeper(@RequestBody Map<String, Object> map) {
        Long repairID = Long.parseLong((String) map.get("ID"));
        boolean success = keeperService.updateRepairKeeper(repairID);
        return JsonResponse.success(success, "success");
    }
    @PostMapping("/getRepairByTypeKeeper")
    @ResponseBody
    public JsonResponse getRepairByTypeKeeper(@RequestBody Dot dot) {
        dot.getData().put("ID", Long.parseLong((String) dot.getData().get("ID")));
        List<RepairForm> repairForms = keeperService.getRepairByTypeKeeper(dot);
        int start = (dot.getPageNo()-1)*dot.getPageSize();
        int end = start+dot.getPageSize();
        Map<String, Object> map = new HashMap<>();
        map.put("totalNum", repairForms.size());
        repairForms = repairForms.subList(start, Math.min(end,repairForms.size()));
        return JsonResponse.success(repairForms, "success").setOtherData(map);
    }
}

